import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path:"/",
        name:"Teacher",
        component:() => import("../views/Teacher.vue")
      },
      {
        path:"/student",
        name:"Student",
        component:() => import("../views/Student.vue")
      },
      {
        path:"/setting",
        name:"Setting",
        component:() => import("../views/Setting.vue")
      },
      {
        path:"/details",
        name:"Details",
        component:() => import("../views/Details.vue")
      }
    ]
  },
  {
    path: '/about',
    name: 'About',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to,from,next)=>{
  var userStatus = JSON.parse(localStorage.getItem("user"))||{};
  //当路径为登录，不需要状态判断
  if(to.name === "Login"){
    next();
  }else{
    if(userStatus.token){
      next();
    }else{
      next("/login")
    }
  }
})

export default router
